<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const email = ref('')
const verifyCode = ref('')
const countdown = ref(0)
const loading = ref(false)
const codeSending = ref(false)

// 发送验证码
const sendVerifyCode = () => {
  if (!email.value) {
    ElMessage.warning('请输入邮箱地址')
    return
  }
  
  if (!/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(email.value)) {
    ElMessage.warning('请输入正确的邮箱格式')
    return
  }
  
  codeSending.value = true
  
  // 这里将来会调用发送验证码API
  setTimeout(() => {
    codeSending.value = false
    ElMessage.success('验证码已发送，请查收邮件')
    startCountdown()
  }, 1000)
}

// 开始倒计时
const startCountdown = () => {
  countdown.value = 60
  const timer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(timer)
    }
  }, 1000)
}

// 注册
const handleRegister = () => {
  if (!email.value) {
    ElMessage.warning('请输入邮箱地址')
    return
  }
  
  if (!verifyCode.value) {
    ElMessage.warning('请输入验证码')
    return
  }
  
  loading.value = true
  
  // 这里将来会调用注册API
  setTimeout(() => {
    loading.value = false
    ElMessage.success('注册成功')
    router.push('/oj/login')
  }, 1000)
}

// 跳转到登录页
const goToLogin = () => {
  router.push('/oj/login')
}
</script>

<template>
  <div class="register-container">
    <div class="register-box">
      <h2 class="title">用户注册</h2>
      <el-form :model="{ email, verifyCode }" label-width="0" size="large">
        <el-form-item>
          <el-input 
            v-model="email" 
            placeholder="请输入邮箱"
            prefix-icon="Message"
            type="email"
          />
        </el-form-item>
        <el-form-item>
          <div class="code-input-container">
            <el-input 
              v-model="verifyCode" 
              placeholder="请输入验证码"
              prefix-icon="Key"
            />
            <el-button 
              type="primary" 
              class="code-btn"
              :disabled="countdown > 0 || codeSending"
              :loading="codeSending"
              @click="sendVerifyCode"
            >
              {{ countdown > 0 ? `${countdown}秒后重试` : '发送验证码' }}
            </el-button>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            class="register-btn" 
            @click="handleRegister"
            :loading="loading"
          >
            注册
          </el-button>
        </el-form-item>
      </el-form>
      <div class="login-link">
        <span>已有账号？</span>
        <el-link type="primary" @click="goToLogin">立即登录</el-link>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
  
  .register-box {
    width: 400px;
    padding: 40px;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    
    .title {
      text-align: center;
      margin-bottom: 30px;
      font-size: 24px;
      color: #333;
    }
    
    .code-input-container {
      display: flex;
      gap: 10px;
      
      .el-input {
        flex: 1;
      }
      
      .code-btn {
        white-space: nowrap;
      }
    }
    
    .register-btn {
      width: 100%;
    }
    
    .login-link {
      text-align: center;
      margin-top: 20px;
      font-size: 14px;
      color: #666;
    }
  }
}
</style>
